<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>报关行自主报价管理</title>
    <jsp:include page="../../head.jsp"/>
</head>
<script type="text/javascript">
    $.offer_bill_no =
            $.offer_bill_no == undefined ? 'declarationOfferAdd' + Math.round(Math.random() * 10000)
                    : $.offer_bill_no;
    //格式化时期yyyy-MM-dd
    function getNowFormatDate(date) {
        var seperator1 = "-";
        var seperator2 = ":";
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        var strDate = date.getDate();
        if (month >= 1 && month <= 9) {
            month = "0" + month;
        }
        if (strDate >= 0 && strDate <= 9) {
            strDate = "0" + strDate;
        }
        var currentdate = year + seperator1 + month + seperator1 + strDate;
        return currentdate;
    }

    //验证自主报价表单必填信息
    function checkdeclarationOffer() {
        var startDate = $("input[name='price_begin_time']")
                .val();//开始时间
        startDate = new Date(startDate.replace(/\-/g, "\/"));
        var endDate = $("input[name='price_end_time']").val();//结束时间
        endDate = new Date(endDate.replace(/\-/g, "\/"));
        var myDate = new Date();
        myDate = getNowFormatDate(myDate);
        startDate = getNowFormatDate(startDate);
        endDate = getNowFormatDate(endDate);

        if ($("input[name='declaration_company']").val() == "") {
            alert("请填写报关公司!");
            $("input[name='declaration_company']").focus();
            return true;
        } else if ($("input[name='declaration_amount']").val() == "") {
            alert("请填写出口报关金额!");
            $("input[name='declaration_amount']").focus();
            return true;
//        } else if ($("input[name='imp_declaration_amount']").val() == "") {
//            alert("请填写进口报关金额!");
//            $("input[name='imp_declaration_amount']").focus();
//            return true;
        } else if ($("input[name='price_begin_time']").val() == "") {
            alert("请选择开始日期!");
            $("input[name='price_begin_time']").focus();
            return true;
        } else if (startDate < myDate) {
            alert("当前日期不能大于开始日期!");
            $("input[name='price_begin_time']").focus();
            return true;
        } else if ($("input[name='price_end_time']").val() == "") {
            alert("请选择结束日期!");
            $("input[name='price_end_time']").focus();
            return true;
        } else if (endDate < startDate) {
            alert("开始时间不能大于结束时间!");
            $("input[name='price_end_time']").focus();
            return true;
        }
        var attaTrs = $($("iframe")[0].contentDocument).find("table tr[id]");
        if (attaTrs.length == 0) {
            alert("您还没有上传附件");
            return true;
        } else if (attaTrs.length > 1) {
            alert("附件数量大于1");
            return true;
        }
    }

    var ID = "${id}";
    var formSetting = {
        title: "查询报关报关行报价信息",
        module: 'save',
        numCount: 2,
        columns: [{
            name: "id",
            label: "id",
            hidden: true
        },
            // 		{
            // 			name : "state",
            // 			label : "审核状态",
            // 			type:"dropdown",
            // 			dropdownType:"ajax",
            // 			url:"../base/dropdown-declarationState",
            // 			op:"eq",
            // 			minLength:0
            // 		},
            {
                name: "declaration_company",
                label: "报价公司",
            }, {
                name: "declaration_amount",
                label: "出口报关金额"
            }, {
                name: "imp_declaration_amount",
                label: "进口报关金额"
            }, {
                name: "container_amount",
                label: "出口免报关费柜数"
            }, {
                name: "imp_container_amount",
                label: "进口免报关费柜数"
            }, {
                name: "price_begin_time",
                type: "date",
                label: "开始时间"
            }, {
                name: "price_end_time",
                type: "date",
                label: "结束时间"
            },
//            {
//                name: "upload_id.id",
//                label: "upload_id",
//                hidden: true
//            },
            {
                index: "review_reason",
                name: "review_reason",
                type: "textarea",
                colspan: "2",
                label: "审核反馈",
                hidden: true
            }, {
                name: "state",
                label: "审核状态",
                hidden: true
            }, {
                name: "declaration_company_id",
                label: "报价公司id",
                hidden: true
            }, {
                name: "remark",
                type: "textarea",
                colspan: "2",
                label: "出口备注"
            }, {
                name: "impRemark",
                type: "textarea",
                colspan: "2",
                label: "进口备注"
            }, {
                name: "com_customs_code",
                label: "报价公司海关代码",
                hidden: false
            },
//            {
//                id: "bid",
//                name: "bid",
//                hidden: true
//            },
            {
                id: "attaTempBillNo",
                name: "attaTempBillNo",
                hidden: true
            }],
        form: {
            'url': 'declarationOffer-saveOrUpdate',
            'data': function () {
                var id = "${id}";
                if (id) {
                    var dataValue = "";
                    $.ajax({
                               url: "declarationOffer-find?id=" + id,
                               async: false,
                               success: function (data) {
                                   dataValue = data;
                               }
                           });
                    return dataValue;
                } else {
                    return null;
                }
            },
            'afterAjaxSuccess': function (responseText, statusText, xhr, $form) {
                $.offer_bill_no = undefined;
                location.href = "declarationOffer-main";
            }
        },
        buttons: {
            custom: [{
                id: "saveBtn",
                value: "保存",
                click: function () {
                    if (checkdeclarationOffer()) {
                        return;
                    }
                    $("input[name='state']").val("OS00");
                    $("#saveButton").click();
                }
            }, {
                id: "submitBtn",
                value: "提交",
                click: function () {
                    if (checkdeclarationOffer()) {
                        return;
                    }
                    $("input[name='state']").val("OS01");
                    $("#saveButton").click();
                }
            }]
        }
    };
    $(document)
            .ready(
                    function () {
                        $("#formtable").mrdForm(formSetting);
                        $("#saveButton").hide();
                        var price_begin_time = $(
                                "input[name='price_begin_time']")
                                .val();
                        $("input[name='price_begin_time']")
                                .val(price_begin_time.substring(0, 10));
                        var price_end_time = $(
                                "input[name='price_end_time']")
                                .val();
                        $("input[name='price_end_time']").val(
                                price_end_time.substring(0, 10));

                        var bill_no = ID == '' ? $.offer_bill_no : ID;
                        $("#attachmentFrame").attr("src",
                                                   "/business/uploadMain?type=offer#"
                                                   + bill_no);
                        $("#attaTempBillNo").val(bill_no);
                        var state = $("input[name='state']")
                                .val();
                        if (state != "") {
                            if (state == "OS00" || state == "OS01") {
                                // 				$(".selector").mrdForm("hide",td_label_16);
                                // 				$(".selector").mrdForm("hide",td_control_16);
                                if (state == "OS01") {
                                    $("#saveBtn").hide();
                                    $("#submitBtn").hide();
                                    $("input[name='state']").attr("readonly",
                                                                  true);
                                    $("#attachmentFrame")[0].onload = function () {
                                        $("#attachmentFrame").contents().find(
                                                "#mf_container0").hide();
                                    };
                                }
                            } else {
                                $("#saveBtn").hide();
                                $("#submitBtn").hide();
                                $("#attachmentFrame")[0].onload = function () {
                                    $("#attachmentFrame").contents().find(
                                            "#mf_container0").hide();
                                };
                            }
                        } else {
// 							$(".selector").mrdForm("hide", td_label_17);
// 							$(".selector").mrdForm("hide", td_control_17);
                        }

                        $
                                .ajax({
                                          url: "../reg-declaraCompany",
                                          dateType: "json",
                                          type: "post",
                                          success: function (data) {
                                              var sourcedata = $.map(data,
                                                                     function (item) {
                                                                         return {
                                                                             label: item[0],
                                                                             id: item[1],
                                                                             com_customs_code: item[2],
                                                                             name: item[0]
                                                                         };
                                                                     });
                                              $(
                                                      "input[name='declaration_company']")
                                                      .autocomplete(
                                                              {
                                                                  minLength: 0,
                                                                  autoFocus: true,
                                                                  delay: 0,
                                                                  source: sourcedata,
                                                                  select: function (event, ui) {
                                                                      var s = ui.item.id;
                                                                      var com_customs_code = ui.item.com_customs_code;
                                                                      $(
                                                                              "input[name='declaration_company_id']")
                                                                              .val(s);
                                                                      $("input[name='com_customs_code']").val(
                                                                              com_customs_code);
                                                                  }
                                                              });
                                          }
                                      });

                    });

    function uploadSuccessBizCallBack(id) {
        $("input[name='upload_id.id']").val(id)
    }
</script>
<body>
<div id="formtable"></div>
<fieldset
        class="replace-ui-jqgrid ui-widget ui-widget-content ui-corner-all mf_con">
    <div
            class="replace-ui-jqgrid-titlebar ui-widget-header ui-corner-top ui-helper-clearfix table_header">
        <a href="javascript:void(0)"
           class="replace-ui-jqgrid-titlebar-close HeaderButton tab_button"><span
                class="ui-icon ui-icon-circle-triangle-s"></span> </a><span
            class="replace-ui-jqgrid-title">报价附件</span>
    </div>
    <iframe id="attachmentFrame" style="width: 100%;" scrolling="no"
            frameborder="0"
            onload="this.height=(this.contentDocument.body.scrollHeight+30)"></iframe>
</fieldset>
</body>
</html>
